package com.guigu.test;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.List;

import com.alibaba.excel.support.ExcelTypeEnum;
import com.guigu.bean.ExcelModel;

/**
 * 单元测试
 *
 * @author Lynch
 */
public class EasyExcelTest2 {
    public static void main(String[] args) throws Exception {
		Instant fromDate = Instant.now();
		writeExcel();
//         readExcel();
//
		Instant toDate = Instant.now();
		long minutes = ChronoUnit.MINUTES.between(fromDate, toDate);
		long hours = ChronoUnit.HOURS.between(fromDate, toDate);
		long seconds = ChronoUnit.SECONDS.between(fromDate, toDate);
		System.err.println("读取耗时===" + seconds);
    }

    /**
     * 写入Excel
     * 
     * @throws FileNotFoundException
     * @author Lynch
     */
    private static void writeExcel() throws FileNotFoundException {
        List<ExcelModel> excelModelList = new ArrayList<>();
		for (int i = 0; i < 10000; i++) {
            ExcelModel excelModel = new ExcelModel();
            excelModel.setAddress("address" + i);
            excelModel.setAge(i + "");
            excelModel.setEmail("email" + i);
            excelModel.setHeigh("heigh" + i);
            excelModel.setLast("last" + i);
            excelModel.setName("name" + i);
            excelModel.setSax("sax" + i);
            excelModelList.add(excelModel);
        }
        
        long beginTime = System.currentTimeMillis();

		String fileName = "D:/tmp/eee" + "_" + DateTimeFormatter.ofPattern("yyyyMMdd").format(LocalDateTime.now())
				+ ".xlsx";
//		OutputStream out = new FileOutputStream("D:/tmp/ccc.xlsx");
		OutputStream out = new FileOutputStream(fileName);
		EasyExcelUtil.writeExcelWithModelBuilder(out, excelModelList, ExcelModel.class, ExcelTypeEnum.XLSX);
        long endTime = System.currentTimeMillis();
        System.out.println(String.format("总共耗时 %s 毫秒", (endTime - beginTime)));
        
        excelModelList = null;
    }

    /**
     * 读取Excel
     * 
     * @throws FileNotFoundException
     * @author Lynch
     */
    private static void readExcel() throws FileNotFoundException {
        try {
            InputStream inputStream=new FileInputStream("D:/aaa.xlsx");
            //读入文件，每一行对应一个 Model，获取 Model 列表
            List<ExcelModel> objectList = EasyExcelUtil.readExcelWithModel(inputStream, ExcelModel.class, ExcelTypeEnum.XLSX);
            for(ExcelModel excelModel: objectList) {
                System.out.println(excelModel);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
 
}